package com.ruoyi.project.system.orderhead.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.changeUtils.YNDateUtlis;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.client.domain.Client;
import com.ruoyi.project.system.client.mapper.ClientMapper;
import com.ruoyi.project.system.client.service.IClientService;
import com.ruoyi.project.system.orderhead.domain.Orderhead;
import com.ruoyi.project.system.orderhead.ex.NoProductException;
import com.ruoyi.project.system.orderhead.mapper.OrderheadMapper;
import com.ruoyi.project.system.orderhead.service.IOrderheadService;
import com.ruoyi.project.system.orderhead.vo.RedPrintVo;
import com.ruoyi.project.system.product.domain.Product;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-28
 */
@Service
public class OrderheadServiceImpl implements IOrderheadService 
{
    @Autowired
    private OrderheadMapper orderheadMapper;
    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private IClientService clientService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param idorderHead 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public Orderhead selectOrderheadById(Integer idorderHead)
    {
        return orderheadMapper.selectOrderheadById(idorderHead);
    }

    @Override
    public Orderhead getOrderhead(Integer idorderHead) {
        return orderheadMapper.getOrderhead(idorderHead);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param orderhead 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Orderhead> selectOrderheadList(Orderhead orderhead)
    {
        return orderheadMapper.selectOrderheadList(orderhead);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param orderhead 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertOrderhead(Orderhead orderhead)
    {
        return orderheadMapper.insertOrderhead(orderhead);
    }

    @Override
    public int addOrderHead(Orderhead orderhead) {
        orderhead.setDateOrderHead(new Date());
        orderhead.setStateOrderHead(3l);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        orderhead.setCreateStaffIDOrderHead(user.getUserId());
        orderhead.setAlterStaffIDOrderHead(user.getUserId());

        //添加客户信息
        Client client = new Client();
        client.setPhoneNum1Client(orderhead.getPhoneOne());
        //client.setClientRegion();
        Client client1 = clientMapper.selectClientByPhone(client.getPhoneNum1Client());
        if(client1!=null){
            Long clientRegion = orderhead.getRegionClient();
            client1.setRegionClient(clientRegion);
            client1.setNameClient(orderhead.getClientName());
            //clientService.updateClient(orderhead.get)
            clientService.updateClient(client1);
            orderhead.setClientIDorderHead(client1.getIdClient().longValue());
        }else{
            client.setRegionClient(orderhead.getRegionClient());
            client.setNameClient(orderhead.getClientName());
            client.setAddressStreetClient(orderhead.getAddress());
            client.setPhoneNum2Client(orderhead.getPhoneTwo());
            clientMapper.insertClient(client);
            orderhead.setClientIDorderHead(client.getIdClient().longValue());
        }
        return orderheadMapper.insertOrderhead(orderhead);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param orderhead 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateOrderhead(Orderhead orderhead)
    {
        return orderheadMapper.updateOrderhead(orderhead);
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteOrderheadByIds(String ids)
    {
        return orderheadMapper.deleteOrderheadByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param idorderHead 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteOrderheadById(Integer idorderHead)
    {
        return orderheadMapper.deleteOrderheadById(idorderHead);
    }

    @Override
    public List<Map<String, Object>> getOrderList(String clientName, String phoneOne, String phoneTwo, Date addStime, Date addEtime,  Date appStime, Date appEtime, Long stateOrderHead,Integer isIndex, Integer idorderHead,Date dateOrderHead,Integer addtime,Integer apptime,Long regionClient) {
        return orderheadMapper.getOrderList(clientName,phoneOne,phoneTwo,addStime,addEtime,appStime,appEtime,stateOrderHead,isIndex,idorderHead,dateOrderHead,addtime,apptime,regionClient);
    }

    @Override
    public List<Map<String, Object>> getReadOrderDataList(Integer idorderHead) {
        return orderheadMapper.getReadOrderDataList(idorderHead);
    }

    @Override
    public int deleteOrderTableByIds(String ids) {
        return orderheadMapper.deleteOrderTableByIds(Convert.toStrArray(ids));
    }

    @Override
    public Double countOrderAllMoney(Integer idorderHead) {
        return orderheadMapper.countOrderAllMoney(idorderHead);
    }

    @Override
    public List<Orderhead> exportOrderHeadList(String clientName, String phoneOne, String phoneTwo, Date dateOrderHead, Date appointmentDateOrderHead, Long stateOrderHead) {
        return orderheadMapper.exportOrderHeadList(clientName,phoneOne,phoneTwo,dateOrderHead,appointmentDateOrderHead,stateOrderHead);
    }

    @Override
    public int updateOrderHeadStateById(Integer idorderHead) {
        return orderheadMapper.updateOrderHeadStateById(idorderHead);
    }

    @Override
    public RedPrintVo printerRedOrder(Integer idorderHead) {
        return orderheadMapper.printerRedOrder(idorderHead);
    }

    @Override
    public int cancellationOrderbyIds(String ids) {
        return orderheadMapper.cancellationOrderbyIds(Convert.toStrArray(ids));
    }

    @Override
    public List<Orderhead> queryDaysOrder() {
        return orderheadMapper.queryDaysOrder();
    }

    @Override
    public List<String> selectOrderheadPrintIdListByDay(String dateOrderHead) {
        return orderheadMapper.selectOrderheadPrintIdListByDay(dateOrderHead);
    }

    @Override
    public Map<String, Object> printSmallOrder(String orderId) {
        RedPrintVo redPrintVo1 = orderheadMapper.printerRedOrder(Integer.parseInt(orderId));
        /*计算总价*/
        if(redPrintVo1==null ){
            return AjaxResult.warn("未添加商品订单,不提供打印服务");
        }
        List<Product> itemList = redPrintVo1.getItemList();
        double sumPriceNum = 0;
        for(Product p :itemList){
            double count = p.getCount();
            Double priceProduct = p.getPriceProduct();
            sumPriceNum += count*priceProduct;
        }
        DecimalFormat df =new java.text.DecimalFormat("#.00");
        String format2 = df.format(sumPriceNum);
        redPrintVo1.setSumPriceNum(format2);
        Map<String,Object> modelMap = new HashMap<>();
        modelMap.put("itemList",itemList);
        //封装农历发货日期
        String dispatchDate = null;
        try{
            dispatchDate = redPrintVo1.getDispatchDate();
            System.out.println("----------------"+dispatchDate);
        }catch (Exception e){
            throw new NoProductException(orderId+"");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String format = simpleDateFormat.format(DateUtils.parseDate(dispatchDate));
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = simpleDateFormat1.format(DateUtils.parseDate(dispatchDate));
        Calendar today = Calendar.getInstance();
        String dateOutOrder = "";
        try {
            today.setTime(simpleDateFormat1.parse(format1));
            String qian = YNDateUtlis.qian(today);
            dateOutOrder = format+" "+qian;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        modelMap.put("date",dateOutOrder);
        //
        modelMap.put("sumPrice",redPrintVo1.getSumPriceNum());
        modelMap.put("order",redPrintVo1);
        System.out.println(modelMap);
        return modelMap;
    }
}
